package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Beans.MsgProblem;
import Utils.HibernateUtil;

public class MsgProblemDao {
	private SessionFactory sessionFactory;

	public MsgProblemDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	
	/**
	 * 删除问题短信
	 */
	public void delete(MsgProblem msgProblem) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(msgProblem);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改问题短信
	 */
	public void edit(MsgProblem msgProblem) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(msgProblem);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的问题短信
	 */
	public void save(MsgProblem msgProblem) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(msgProblem);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有问题短信信息
	 */
	public List<MsgProblem> getAllMsgProblem(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(MsgProblem.class);
		List<MsgProblem> msgProblems=c.list();
		tr.commit();
		session.close();
		return msgProblems;
	}
	
	/**
	 * 通过问题短信ID返回指定问题短信信息
	 * 
	 * @return
	 */
	public MsgProblem getMsgProblemById(int msgProblemId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		MsgProblem msgProblem = (MsgProblem) session.get(MsgProblem.class, msgProblemId);
		tr.commit();
		session.close();
		return msgProblem;
	}
}
